package com.renchunlin.taobaounion.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.renchunlin.taobaounion.R;
import com.renchunlin.taobaounion.base.BaseFragment;
import com.renchunlin.taobaounion.model.domain.IBaseInfo;
import com.renchunlin.taobaounion.model.domain.OnSellContent;
import com.renchunlin.taobaounion.presenter.IOnSellPagePresenter;
import com.renchunlin.taobaounion.presenter.ITicketPresenter;
import com.renchunlin.taobaounion.ui.activity.TicketActivity;
import com.renchunlin.taobaounion.ui.adapter.OnSellContentAdapter;
import com.renchunlin.taobaounion.utils.PresenterManager;
import com.renchunlin.taobaounion.utils.SizeUtils;
import com.renchunlin.taobaounion.utils.TicketUtil;
import com.renchunlin.taobaounion.utils.ToastUtil;
import com.renchunlin.taobaounion.view.IOnSellPageCallback;

import java.util.List;

import butterknife.BindView;

public class OnSellFragment extends BaseFragment implements IOnSellPageCallback {

    private IOnSellPagePresenter mOnSellPagePresenter;

    @BindView(R.id.on_sell_content_list)
    public RecyclerView mContentRv;

    @BindView(R.id.on_sell_refresh_layout)
    public TwinklingRefreshLayout mTwinklingRefreshLayout;

    private static final int DEFAULT_SPAN_COUNT = 2;
    private OnSellContentAdapter mOnSellContentAdapter;

    @BindView(R.id.fragment_bar_title_tv)
    public TextView barTitleTv;

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_on_sell;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_with_bar_layout, container, false);
    }

    @Override
    protected void initView(View rootView) {
        //设置布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), DEFAULT_SPAN_COUNT);
        mContentRv.setLayoutManager(gridLayoutManager);
        mOnSellContentAdapter = new OnSellContentAdapter();
        mContentRv.setAdapter(mOnSellContentAdapter);
        mContentRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.bottom = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.left = SizeUtils.dip2px(getContext(), 2.5f);
                outRect.right = SizeUtils.dip2px(getContext(), 2.5f);
            }
        });

        mTwinklingRefreshLayout.setEnableLoadmore(true);
        mTwinklingRefreshLayout.setEnableRefresh(false);

        barTitleTv.setText("特惠宝贝");
    }

    @Override
    protected void initPresenter() {
        mOnSellPagePresenter = PresenterManager.getInstance().getOnSellPagePresenter();
        mOnSellPagePresenter.registerViewCallback(this);
        mOnSellPagePresenter.getOnSellContent();
    }

    @Override
    protected void initListener() {
        mTwinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                //去加载更多内容
                if (mOnSellPagePresenter != null) {
                    mOnSellPagePresenter.loadMore();
                }
            }
        });

        mOnSellContentAdapter.setOnSellPageItemClickListener(new OnSellContentAdapter.OnSellPageItemClickListener() {
            @Override
            public void onSellItemClick(IBaseInfo data) {
                TicketUtil.toTicketPage(getContext(), data);
            }
        });
    }

    @Override
    protected void release() {
        if (mOnSellPagePresenter != null) {
            mOnSellPagePresenter.unregisterViewCallback(this);
        }
    }

    @Override
    public void onContentLoadedSuccess(OnSellContent result) {
        setUpState(State.SUCCESS);
        //数据回来了
        //更新ui
        mOnSellContentAdapter.setData(result);
    }

    @Override
    public void onMoreLoaded(OnSellContent moreResult) {
        if (mTwinklingRefreshLayout != null) {
            mTwinklingRefreshLayout.finishLoadmore();
        }
        //加载更多的内容，添加内容到适配器里
        mOnSellContentAdapter.setMoreLoaded(moreResult);
        List<OnSellContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data = moreResult.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
        ToastUtil.showToast("加载了" + map_data.size() + "个宝贝");
    }

    @Override
    public void onMoreLoadedError() {
        if (mTwinklingRefreshLayout != null) {
            mTwinklingRefreshLayout.finishLoadmore();
        }
        ToastUtil.showToast("网络异常，请稍后重试...");
    }

    @Override
    public void onMoreLoadedEmpty() {
        if (mTwinklingRefreshLayout != null) {
            mTwinklingRefreshLayout.finishLoadmore();
        }
        ToastUtil.showToast("没有更多内容...");
    }

    @Override
    public void onError() {
        setUpState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setUpState(State.LOADING);
    }

    @Override
    public void onEmpty() {
        setUpState(State.EMPTY);
    }
}
